import 'dart:convert';

import 'package:flutter/material.dart';
import '../common/Constants.dart';
import '../model/MainModel.dart';
import 'WebViewPage.dart';
import 'main.dart';

// ignore: must_be_immutable
class MainPage extends StatefulWidget {
  PageController controller;

  MainPage({this.controller});

  @override
  State<StatefulWidget> createState() {
    return MainState();
  }
}

class MainState extends State<MainPage> {
  List<dynamic> data = [];
  static final String KEY_SETTING = "setting";
  static final String KEY_MINE = "mine";

  @override
  void initState() {
    super.initState();
    loadData();
  }

  @override
  Widget build(BuildContext context) {
    return Stack(
      children: <Widget>[
        PageView.builder(
            itemCount: data.length,
            scrollDirection: Axis.vertical,
            itemBuilder: (context, index) {
              return InkWell(
                child: Container(
                    color: Colors.white,
                    child: Column(
                      mainAxisSize: MainAxisSize.max,
                      children: <Widget>[
                        FadeInImage.assetNetwork(
                          placeholder: Constants.LOCAL_BIG_IMAGE,
                          image: (data[index] as Datas).thumbnail,
                          width: double.infinity,
                          height: 220,
                          fit: BoxFit.cover,
                        ),
                        Expanded(
                          child: Padding(
                            padding: EdgeInsets.symmetric(horizontal: 15),
                            child: Column(
                              children: <Widget>[
                                Container(
                                    width: 100,
                                    height: 20,
                                    margin: EdgeInsets.only(bottom: 25),
                                    alignment: Alignment.center,
                                    color: Color.fromRGBO(156, 119, 66, 1),
                                    child: Text(
                                      "T O   R E A D",
                                      style: TextStyle(
                                          color: Colors.white, fontSize: 10),
                                    )
                                ),
                                Expanded(
                                  child: Container(
                                    width: 200,
                                    alignment: Alignment.center,
                                    child: Text((data[index] as Datas).title,
                                        textAlign: TextAlign.center,
                                        style: TextStyle(fontSize: 35)
                                    ),
                                  )
                                ),
                                Container(
                                  margin: EdgeInsets.only(top: 25, bottom: 30),
                                  child: Text(
                                    (data[index] as Datas).excerpt,
                                    maxLines: 4,
                                    overflow: TextOverflow.ellipsis,
                                    style: TextStyle(fontSize: 15),
                                  ),
                                ),
                                Container(
                                    padding: EdgeInsets.only(left: 40, right: 40),
                                    margin: EdgeInsets.only(bottom: 20),
                                    child: Divider(height: 2)),
                                Text(
                                  (data[index] as Datas).author,
                                  style: TextStyle(fontSize: 16),
                                ),
                              ],
                            )
                          )
                        ),
                        SizedBox(
                            height: 70,
                            child: Padding(
                                padding: EdgeInsets.symmetric(horizontal: 15),
                                child: Row(
                                  children: <Widget>[
                                    Container(
                                      width: 15,
                                      height: 15,
                                      child:
                                          Image.asset("image/icon_comment.png"),
                                    ),
                                    Text((data[index] as Datas).comment,
                                        style: TextStyle(fontSize: 12)),
                                    Padding(
                                      padding: EdgeInsets.only(right: 8),
                                    ),
                                    Container(
                                      width: 15,
                                      height: 15,
                                      child: Image.asset("image/icon_keep.png"),
                                    ),
                                    Text((data[index] as Datas).good,
                                        style: TextStyle(fontSize: 12)),
                                    Expanded(
                                      child: Row(
                                        mainAxisAlignment:
                                            MainAxisAlignment.end,
                                        children: <Widget>[
                                          Text("阅读数",
                                              style: TextStyle(
                                                  color: Color(0xff999999),
                                                  fontSize: 12)),
                                          Padding(
                                            padding: EdgeInsets.only(right: 6),
                                          ),
                                          Text(
                                            (data[index] as Datas).view,
                                            style: TextStyle(fontSize: 12),
                                          )
                                        ],
                                      ),
                                    )
                                  ],
                                )))
                      ],
                    )),
                onTap: () {
                  Navigator.push(
                      context,
                      MaterialPageRoute(
                          builder: (context) => WebViewPage(
                              title: (data[index] as Datas).title,
                              url: (data[index] as Datas).html5,
                              id: (data[index] as Datas).id)));
                },
              );
            }),
        Padding(
            padding: EdgeInsets.only(left: 20, right: 20, top: 20, bottom: 20),
            child: Row(
              children: <Widget>[
                IconButton(
                    icon: Icon(
                      Icons.menu,
                      color: Colors.white,
                    ),
                    onPressed: () {
                      onClick(KEY_SETTING);
                    }),
                Expanded(
                    child: Text(
                  "单读",
                  textAlign: TextAlign.center,
                  style: TextStyle(color: Colors.white),
                )),
                IconButton(
                    icon: Icon(
                      Icons.supervised_user_circle,
                      color: Colors.white,
                    ),
                    onPressed: () {
                      onClick(KEY_MINE);
                    })
              ],
            ))
      ],
    );
  }

  void loadData() async {
    var str =
        await DefaultAssetBundle.of(context).loadString("json/HomeData.json");
    var model = MainModel.fromJson(json.decode(str));
    setState(() {
      data = model.datas;
    });
  }

  void onClick(String key) {
    if (KEY_SETTING == key) {
      widget.controller.animateToPage(HomePage.PAGE_SETTING,
          duration: Duration(milliseconds: 1500), curve: ElasticOutCurve(2));
    } else {
      widget.controller.animateToPage(HomePage.PAGE_MINE,
          duration: Duration(milliseconds: 1500), curve: ElasticOutCurve(2));
    }
  }
}
